#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h> 

#define high 25
#define width 30
int food_x,food_y;	 
int canvas[high][width]={0};		//0是空格,-1是边框#,-2是食物F,>1是蛇身*,1是蛇头@
int moveDirection;		//小蛇移动方向 



void HideCursor()//隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}



void gotoxy(int x,int y)		//类似于清屏函数，光标移动到原点位置进行重画
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}




//数据初始化 
void starup() 
{
	HideCursor();	 
	moveDirection=2;		
	food_x=4;
	food_y=4;
	canvas[food_x][food_y]=-2; 
	int i,j;
	for(i=0;i<width;i++) 
	{
		canvas[0][i]=-1;
		canvas[high-1][i]=-1;
	}
	
	for(i=0;i<high;i++) 
	{
		canvas[i][0]=-1;
		canvas[i][width-1]=-1;
	}
	canvas[high/2][width/2]=1;
	for(i=1;i<=4;i++) 
	{
		canvas[high/2][width/2-i]=i+1;
	} 

}



void moveSnakeDirection() 
{
	int i,j;
	int max=0;
	int oldTail_i,oldTail_j;
	int oldHead_i,oldHead_j;
	int newHead_i,newHead_j;
	
	for(i=1;i<high-1;i++) 
	{
		for(j=1;j<width-1;j++) 
		{
			if(canvas[i][j]>0) 
			{
				canvas[i][j]=canvas[i][j]+1;
				if(max<canvas[i][j]) 
				{
					max=canvas[i][j];
					oldTail_i=i;
					oldTail_j=j;
				}
				if(canvas[i][j]==2) 
				{
					oldHead_i=i;
					oldHead_j=j;
				}
			}
		}
	}
	
	
	if(moveDirection==1) 
	{		//向上 
		newHead_i=oldHead_i-1;
		newHead_j=oldHead_j;
	}
	if(moveDirection==2) 
	{		//向下 
		newHead_i=oldHead_i+1;
		newHead_j=oldHead_j;
	}
	if(moveDirection==3) 
	{		//向左 
		newHead_i=oldHead_i;
		newHead_j=oldHead_j-1;
	}
	if(moveDirection==4) 
	{		//向右 
		newHead_i=oldHead_i;
		newHead_j=oldHead_j+1;
	}
	if(canvas[newHead_i][newHead_j]==-2) 
	{
		canvas[food_x][food_y]==0;
		while(1) 
		{
			food_x=rand()%(high-2)+1;
			food_y=rand()%(width-2)+1;
			if(canvas[food_x][food_y]==0)
			{
				canvas[food_x][food_y]=-2;
				break;
			}
		}
	} 
	else canvas[oldTail_i][oldTail_j]=0;
	if(canvas[newHead_i][newHead_j]!=0&&canvas[newHead_i][newHead_j]!=-2) 
	{
		printf("game over!");
		exit(0);
	} 
	else 
	{
		canvas[newHead_i][newHead_j]=1;
	}
}




void show() 
{
	gotoxy(0,0);	
	int i,j;
	for(i=0;i<high;i++) 
	{
		for(j=0;j<width;j++) 
		{
			if(canvas[i][j]==-1) printf("#");
			else if(canvas[i][j]==1) printf("@");
			else if(canvas[i][j]==-2) printf("F");
			else if(canvas[i][j]==0) printf(" ");
			else if(canvas[i][j]>1) printf("*");
		}
		printf("\n");
	}
	
}



void updateWithoutInput() 
{
	moveSnakeDirection();
	Sleep(50);
} 



void updateWithInput() 
{	
	char input;
	if(kbhit())
	{
		input=getch();
		if(input=='w') moveDirection=1;
		if(input=='s') moveDirection=2;
		if(input=='a') moveDirection=3;
		if(input=='d') moveDirection=4;
	}
	
}
int main() 
{
	starup();
	while(1) 
	{
		show();
		updateWithoutInput();
		updateWithInput();	
	}
	return 0;
}